<?php
namespace Admin\Controller;
use think\controller;
use Think\Image;

class ArticleController extends AuthController{
    /**
     *  文章列表
     */
    private $categoryModel;
    private $ArtcleModel;
    public function _initialize(){
        $this->categoryModel = new \Common\Model\CategoryModel();
        //这是page页面的
        $this->ArtcleModel= new \Common\Model\ArticleModel();
    }
    public function index(){
        $this->page_title="بارلىق خەۋەرلەر";
        //自定义函数,page分页信息
        function N($val){
            $a = M('configs')->field('value')->select();
            return $a[$val]['value'];
        }
        $Page       = new \Think\Page($this->ArtcleModel->count(),N(0));// 实例化分页类 传入总记录数和每页显示的记录数(10)
        $Page->setConfig('next','كىيىنكى بەت');
        $Page->setConfig('prev','ئالدىنقى بەت');
        $this->sql=$this->ArtcleModel->order('aid desc')->field('aid,category_cid,title,status,author,thumb,addTime')->select();
        $sql=$this->ArtcleModel
                            ->order('aid desc')
                            ->join('blog_category ON page.category_cid = blog_category.cid')
                            ->field('aid,category_cid,title,page.status,author,thumb,addTime,cname')
                             ->limit($Page->firstRow.','.$Page->listRows)
                            ->select();
        $this->assign('sql',$sql);   // 赋值数据集
        $this->assign('page',$Page->show());// 赋值分页输出
        $this->display();
    }
    //添加文章
    public function add()
    {

        $this->page_title=" خەۋەر قوشۇش";
        //所有标签数据
        $this->categorys=$this->categoryModel->gettrue();
        //所有tag标签
        $this->tags=M('tag')->field('tid,tname')->select();
        $this->display();
    }

    //提交文章表单
    public function addhend()
    {
        $post=I('POST.');
        $post['addTime']=time();
        if(!$this->ArtcleModel->addArticle($post)) $this->error($this->ArtcleModel->getError());
        header("location:index.html");
    }


    //封面图片
    public function uploadThumb()
    {
        $config = array(
            'maxSize'    =>    3145728,
            'rootPath'   =>    './Uploads/',  //根目录
            'savePath'   =>    'Article/',    //根目录下的目录
            'saveName'   =>    array('uniqid',''),
            'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
            'autoSub'    =>    true,
            'subName'    =>    array('date','Ymd'),
        );
        $upload = new \Think\Upload($config);// 实例化上传类
        //上传图片
        $info=$upload->upload();
        if(!$info) $this->error($upload->getError());
        $thumb=$info['Filedata'];
        $savepath=$thumb['savepath'];
        $savename=$thumb['savename'];
        $pathSave='Uploads/'.$savepath.$savename;  //图片的完美地址

        //处理图片大小
        $image=new Image();
        //打开图像资源
        $image->open($pathSave);
        //裁剪图片
        $image->thumb(260,180,6)->save($pathSave);
        $this->ajaxReturn($pathSave);

    }
    //删除文章
    public function delete()
    {
        $aid=I('post.aid');
        //0.删除文章相关的图片
        $thumb=$this->ArtcleModel->where(array('aid'=>$aid))->getField('thumb');  //图片的地址
        // is_file() 函数检查指定的文件名是否是正常的文件
        // file_exists() 函数检查文件或目录是否存在
        //unlink()  函数删掉指定的文件
        if(is_file($thumb) && file_exists($thumb)) unlink($thumb);
        //1.删除page
        if($this->ArtcleModel->where(array('aid'=>$aid))->delete()){
            //2.删除page_tag
            M('page_tag')->where(array('aid'=>$aid))->delete();
            $data=['status'=>1,'massage'=>'ئۆچۈرۈش تامام'];
        }
        else{
            $data=['status'=>0,'massage'=>'مەغلۇب بولدى،قايتا سىناڭ!'];
        }
        $this->ajaxReturn($data);

    }

    //编辑文章
    public function edit($aid)
    {
        $this->page_title="ئەسەر تەھرىرلەش";
        $this->categorys=$this->categoryModel->gettrue();
        $this->pags=$this->ArtcleModel->where(array('aid'=>$aid))->find();
        //分配所有标签数据
        $this->tags=M('tag')->select();
        //当前文章的tag
        $this->page_tags=M('page_tag')->where(array('aid'=>$aid))->getField('tid',true);
        $this->display();
    }

    //文章编辑完成后提交
    public function savehend()
    {
        $post=I('post.');
        //更新page信息
        if(!$this->ArtcleModel->save($post)) $this->error('编辑失败！');
        //更新tag表
        M('page_tag')->where(array('aid'=>$post['aid']))->delete();
        if(isset($post['tag']) && is_array($post['tag'])){
            foreach ($post['tag'] as $tag){
                $tags=[
                    'aid'=>$post['aid'],
                    'tid'=>$tag
                ];
                M('page_tag')->add($tags);
            }
        }
        $this->success('编辑成功了',U("Article/index"),1);
    }

}